import { getParams } from "@/libs/util";
import Taro from "@tarojs/taro";
import { getUploadSignature } from "_api/base-api";
import { getFormDataParams } from "../libs/upload";

const MAX_NUMBER = 990000;
const NEED_SHIFT = 10000;
export default {
  filters: {
    /* 格式化点赞数和花瓣数 */
    formatNumber(value = 0) {
      value = Math.abs(value);
      if (value < NEED_SHIFT) return value;
      let formatValue = value;
      formatValue = Math.min(
        Math.ceil(Math.abs(value) / NEED_SHIFT),
        MAX_NUMBER
      );
      if (formatValue >= MAX_NUMBER) {
        formatValue = (MAX_NUMBER / NEED_SHIFT).toFixed(1) + "w+";
      } else {
        formatValue = formatValue.toFixed(1) + "w";
      }
      return formatValue;
    }
  },
  data() {
    return {
      is_opened: false, // 是否打卡提示窗
      toastText: "", // 提示文案
      isLoading: false,
      isShowModal: false,
      
    };
  },
  computed: {
    /* 获取页面参数 */
    getParams() {
      return getParams();
    }
  },

  methods: {
    /**
     * @description 上传大小限制判断
     * @param {array} imgArr  [{ path: String , size: Number }] 图片数组
     * @param {object} { maxSize: null, } 最大限制单位: M
     */
    uploadFileSizeIsLegal(imgArr, { maxSize = 10 }) {
      return imgArr.every(item => {
        return item.size <= maxSize * 1024 * 1024;
      });
    },
    /* 上传文件 */
    async uploadFile(filePath: string, callback, errorCallback) {
      try {
        const data = await getFormDataParams(filePath)
        Taro.uploadFile({
          url: `${data.host}`,
          filePath,
          name: "file",
          formData: data,
          success: res => {
            callback(
              {
                url: data.host + data.key,
                msg: "ok",
                code: 200
              },
              res,
              data
            );
          },
          fail: res => {
            console.log(res, "upload fail");
            /*  console.log(res); */
            errorCallback({ msg: "fail", url: null, code: 400 }, res, data);
          }
        });
      } catch (error) {
        console.log(error,'getFormDataParamsError')
        errorCallback({ msg: "fail", code: 400 });
      }
    },

    /**关闭提示的方法 */
    handleCloseToast() {
      this.is_opened = false;
    },
    /**弹窗管理 */
    handleModalCancel() {
      this.isShowModal = false;
    },
    /**toast提示方法 */
    $message(message, icon) {
      // this.is_opened = true;
      // this.toastText = message;
      if (!message) return;
      icon = icon || "none";
      Taro.showToast({ title: message, icon, duration: 2000 });
    },
    $startLoading(title = "加载中") {
      Taro.showLoading({ title });
    },
    $endLoading() {
      Taro.hideLoading();
    }
  }
};
